/*
 * Copyright 2016 Stuart Kent
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.example.mybugshaker.flow.email;

import com.example.mybugshaker.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public final class FeedbackEmailIntentProvider {
    @NotNull
    private final Context context;

    @NotNull
    private final GenericEmailIntentProvider genericEmailIntentProvider;

    @NotNull
    private final App app;

    @NotNull
    private final Environment environment;

    @NotNull
    private final Device device;
    private final HiLogLabel lable = new HiLogLabel(HiLog.LOG_APP, 0x00201, "FeedbackEmailIntentProvider");

    public FeedbackEmailIntentProvider(
            @NotNull final Context fcontext,
            @NotNull final GenericEmailIntentProvider fgenericEmailIntentProvider) {

        this.context = fcontext;
        this.genericEmailIntentProvider = fgenericEmailIntentProvider;
        this.app = new App(fcontext);
        this.environment = new Environment();
        this.device = new Device(fcontext);
    }

    @NotNull
        /* default */ Intent getFeedbackEmailIntent(
            @NotNull final String[] emailAddresses,
            @Nullable final String userProvidedEmailSubjectLine) {

        final String emailSubjectLine = getEmailSubjectLine(userProvidedEmailSubjectLine);
        final String emailBody = getApplicationInfoString(app,environment,device);

        return genericEmailIntentProvider
                .getEmailIntent(emailAddresses, emailSubjectLine, emailBody);
    }

    @NotNull
        /* default */ Intent getFeedbackEmailIntent(
            @NotNull final String[] emailAddresses,
            @Nullable final String userProvidedEmailSubjectLine,
            @NotNull final Uri screenshotUri) {

        final String emailSubjectLine = getEmailSubjectLine(userProvidedEmailSubjectLine);
        final String emailBody = getApplicationInfoString(app, environment, device);

        return genericEmailIntentProvider
                .getEmailWithAttachmentIntent(
                        emailAddresses, emailSubjectLine, emailBody, screenshotUri);
    }

    public String getApplicationInfoString() {
            return this.getApplicationInfoString(app, environment, device);
    }

    @NotNull
    private String getApplicationInfoString(
            @NotNull final App fapp,
            @NotNull final Environment fenvironment,
            @NotNull final Device fdevice) {

        final String versionString = String.format(
                "%s (%s)",
                fenvironment.getVersionName(),
                fenvironment.getVersionCode()
        );

        final String appVersionString = String.format(
                "%s (%s)",
                fapp.getVersionName(),
                fapp.getVersionCode()
        );
        return
                getSourceFileString(ResourceTable.String_bugshaker_email_body_time_stamp, getCurrentUtcTimeStringForDate(new Date())) +
                getSourceFileString(ResourceTable.String_bugshaker_email_body_app_version, appVersionString) +
                getSourceFileString(ResourceTable.String_bugshaker_email_body_install_source, app.getInstallSource().toString()) +
                getSourceFileString(ResourceTable.String_bugshaker_email_body_OpenHarmony_version, versionString) +
                getSourceFileString(ResourceTable.String_bugshaker_email_body_device_manufacturer, fdevice.getManufacturer()) +
                getSourceFileString(ResourceTable.String_bugshaker_email_body_device_model, fdevice.getModel()) +
                getSourceFileString(ResourceTable.String_bugshaker_email_body_display_resolution, fdevice.getResolution()) +
                getSourceFileString(ResourceTable.String_bugshaker_email_body_display_density_actual,  fdevice.getActualDensity()) +
                getSourceFileString(ResourceTable.String_bugshaker_email_body_display_density_bucket, fdevice.getDensityBucket()) +
                "---------------------\n\n";
    }

    private String getSourceFileString(int sourceId, String extString) {
        try {
            return context.getResourceManager().getElement(sourceId).getString() + extString + "\n";
        } catch (IOException e) {
            HiLog.error(lable,"FeedbackEmailIntentProvider", e);
            return null;
        } catch (NotExistException e) {
            HiLog.error(lable,"FeedbackEmailIntentProvider", e);
            return null;
        } catch (WrongTypeException e) {
            HiLog.error(lable,"FeedbackEmailIntentProvider", e);
            return null;
        }
    }

    @NotNull
    private String getEmailSubjectLine(@Nullable final String userProvidedEmailSubjectLine) {
        if (userProvidedEmailSubjectLine != null) {
            return userProvidedEmailSubjectLine;
        }

        return context.getString(
                ResourceTable.String_bugshaker_email_default_subject_line,
                app.getName()
        );
    }

    @NotNull
    private String getCurrentUtcTimeStringForDate(final Date date) {
        final SimpleDateFormat simpleDateFormat
                = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z", Locale.getDefault());

        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        return simpleDateFormat.format(date);
    }

}
